package com.lisn.roomlib;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.lisn.roomlib.FileInfo.FileInfo;
import com.lisn.roomlib.FileInfo.FileInfoDatabase;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

public class RoomTestActivity extends AppCompatActivity {

    private static final String TAG = RoomTestActivity.class.getSimpleName();
    private FileInfoDatabase fileInfoDatabase;
    private String name;
    private FileViewModel fileViewModel;
    private FileInfo fileInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_room_test);

        fileInfoDatabase = FileInfoDatabase.get();

        fileViewModel = new ViewModelProvider(this).get(FileViewModel.class);
        fileViewModel.getAllFileInfos().observe(this, new Observer<List<FileInfo>>() {
            @Override
            public void onChanged(List<FileInfo> fileInfos) {
                Log.e(TAG, "onChanged: " + fileInfos);
            }
        });
    }

    public static void startActivity(Context context) {
        context.startActivity(new Intent(context, RoomTestActivity.class));
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    public void zeng(View view) {
        FileInfo fileInfo = new FileInfo();
        name = "img" + new Random().nextLong();
        fileInfo.name = name;
        String path = Environment.getDataDirectory().getAbsolutePath();
        fileInfo.path = path + "/" + fileInfo.name;
        String format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date());
        fileInfo.time = format;
        fileInfo.type = "img";
        fileInfo.age = 18;
        fileInfo.thumbnail = path + "/img";
        fileInfoDatabase.getFileInfoDao().save(fileInfo);
    }

    public void shan(View view) {
        if (fileInfo == null) {
            return;
        }
        name = "";

        fileInfoDatabase.getFileInfoDao().delete(fileInfo);
    }

    public void gai(View view) {
        if (TextUtils.isEmpty(name)) {
            return;
        }
        FileInfo fileInfo = fileInfoDatabase.getFileInfoDao().getFileInfo(name);
        fileInfo.path = "/path12345678/" + fileInfo.name;
        fileInfoDatabase.getFileInfoDao().update(fileInfo);
    }

    public void cha(View view) {
        if (TextUtils.isEmpty(name)) {
            return;
        }
        fileInfo = fileInfoDatabase.getFileInfoDao().getFileInfo(name);
        Log.e(TAG, "cha: " + fileInfo);
    }

    public void all(View view) {
        List<FileInfo> fileInfos = fileInfoDatabase.getFileInfoDao().loadAllFileInfo();
        Log.e(TAG, "all: " + fileInfos);
    }
}